﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace TPCN.DAL
{
   public class NhanVienDAL
    {
        public static NhanVienObj GetByID(string NhanvienID)
        {
            NhanVienObj obj = new NhanVienObj();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "sp_tbNhanVien_GetByID";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@NhanvienID", NhanvienID);
                using (SqlDataReader dr = Database.db.GetDataToDataReader(cmd))
                {
                    if (dr.HasRows)
                    {
                        dr.Read();
                        obj = obj.SetValuesFromDataReader(dr);
                    }
                    dr.Close();
                }
            }
            return obj;
        }

        public static List<NhanVienObj> GetByAll()
        {
            List<NhanVienObj> result = new List<NhanVienObj>();
            NhanVienObj obj = new NhanVienObj();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "sp_tbNhanVien_GetByAll";
                cmd.CommandType = CommandType.StoredProcedure;
                using (SqlDataReader dr = Database.db.GetDataToDataReader(cmd))
                {
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            result.Add(obj.SetValuesFromDataReader(dr));
                        }
                    }
                    dr.Close();
                    obj = null;
                }
            }
            return result;
        }

        public static bool Insert(NhanVienObj obj)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "sp_tbNhanVien_Insert";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Hovaten", (obj.Hovaten == null) ? String.Empty : obj.Hovaten);
                    cmd.Parameters.AddWithValue("@Email", (obj.Email == null) ? String.Empty : obj.Email);
                    cmd.Parameters.AddWithValue("@Username", (obj.Username == null) ? String.Empty : obj.Username);
                    cmd.Parameters.AddWithValue("@Password", (obj.Password == null) ? String.Empty : obj.Password);
                    cmd.Parameters.AddWithValue("@IsAdmin", (obj.IsAdmin == null) ? String.Empty : obj.IsAdmin);
                    cmd.Parameters.AddWithValue("@Active", (obj.Active == null) ? String.Empty : obj.Active);
                    Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool Update(NhanVienObj obj)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "sp_tbNhanVien_Update";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@NhanvienID", (obj.NhanvienID == null) ? String.Empty : obj.NhanvienID);
                    cmd.Parameters.AddWithValue("@Hovaten", (obj.Hovaten == null) ? String.Empty : obj.Hovaten);
                    cmd.Parameters.AddWithValue("@Email", (obj.Email == null) ? String.Empty : obj.Email);
                    cmd.Parameters.AddWithValue("@Username", (obj.Username == null) ? String.Empty : obj.Username);
                    cmd.Parameters.AddWithValue("@Password", (obj.Password == null) ? String.Empty : obj.Password);
                    cmd.Parameters.AddWithValue("@IsAdmin", (obj.IsAdmin == null) ? String.Empty : obj.IsAdmin);
                    cmd.Parameters.AddWithValue("@Active", (obj.Active == null) ? "0" : obj.Active);
                    Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool Delete(string NhanvienID)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "sp_tbNhanVien_DeleteByID";
                    cmd.Parameters.AddWithValue("@NhanvienID", NhanvienID);
                    Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
